<?php
class Core_ACL extends Zend_Db_Table_Abstract {

    protected $_name = 'db_controller';

    /*
     * Controllers
     */

    //get All Controllers
    public function getControllers() {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('c' => 'db_controller'))
                       ->order('c.module');
        return $this->_fetch($select);
    }
    
    //Get Controllers by Name of Controller
    public function getControllerByName($name) {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('c' => 'db_controller'))
                       ->where('c.controller = ?', $name);
        return $this->_fetch($select);
    }

    //Get Controllers by name of controller and module
    public function getControllersByControllerModule($controller,$module) {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('c' => 'db_controller'))
                       ->where('c.controller = ?', $controller)
                       ->where('c.module = ?', $module);
        return $this->_fetch($select);
    }
    
    //Get controller unused (pass contollers used)
    public function getControllersUnused($controllers) {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('c' => 'db_controller'))
                       ->where("c.controller NOT IN ('" . implode("','",$controllers) . "') ");
        return $this->_fetch($select);
    }
    
    //get count of controllers
    public function getControllersCount() {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('a' => 'db_action'), array('counController' => 'count(*)', 'id_controller'))
                       ->group('a.id_controller');
        return $this->_fetch($select);
    }

    //Update Controller
    public function updateController($idController,$data)
    {
        return $this->_db->update('db_controller', $data, 'id = ' . $idController);
    }

    //Insert Controller
    public function insertController($controller) {
        $arrayController = explode("_", $controller);
        if (count($arrayController) == 1) {
            $insert['module'] = 'default';
        } else {
            $insert['module'] = $arrayController[0];
        }
        $insert['controller'] = $controller;
        $this->_db->insert('db_controller', $insert);
        return $this->_db->lastInsertId();
    }

    //Delete controller by ID
    public function deleteControllerByID($id) {
        $this->_db->delete('db_controller', 'id = ' . $id);
    }

    /*
     * Actions
     */

    //Get Action
    public function getAction() {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('a' => 'db_action'));
        return $this->_fetch($select);
    }

    //Get Action by ID Contoller
    public function getActionByIDController($id) {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('a' => 'db_action'))
                       ->where('a.id_controller = ?', $id);
        return $this->_fetch($select);
    }

     //Get Action by Action and ID Controller
    public function getAccionByAcionIDControlador($action, $idController) {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('a' => 'db_action'))
                       ->where('a.action = ?', $action)
                       ->where('a.id_controller = ?', $idController);
        return $this->_fetch($select);
    }

    //Get Action unused
    public function getActionUnused($actions, $idController) {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('a' => 'db_action'))
                       ->where('a.id_controller = ?', $idController)
                       ->where("a.action NOT IN ('" . implode("','",$actions) . "') ");
        return $this->_fetch($select);
    }

    //Insert Action
    public function insertAction($action, $idController) {
        $insert = array('action' => $action, 'id_controller' => $idController);
        $this->_db->insert('db_action', $insert);
        return $this->_db->lastInsertId();
    }

    //Delete Action by ID Controller
    public function deleteActionByIDController($id) {
        $this->_db->delete('db_action', 'id_controller = ' . $id);
    }

    //Delete Action by ID Action
    public function deleteActionById($id) {
        $this->_db->delete('db_action', 'id = ' . $id);
    }

    /*
     * Controllers & Actions
     */

    //Get Controllers and Actions
    public function getControllerAction() {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('c' => 'db_controller'))
                       ->join(array('a' => 'db_action'), 'c.id = a.id_controller')
                       ->order('c.module');
        return $this->_fetch($select);
    }

    //Get Controllers And Actions by ID Controller
    public function getControllerActionByIDController($id) {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('c' => 'db_controller'))
                       ->join(array('a' => 'db_action'), 'c.id = a.id_controller')
                       ->where('c.id = ?', $id);
        return $this->_fetch($select);
    }

    /*
     * ACL
     */

    //Get ACL
    public function getACL() {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('p' => 'db_ACL'));
        return $this->_fetch($select);
    }

    //Get All ACL
    public function getAllACL() {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('c' => 'db_controller'))
                       ->join(array('a' => 'db_action'), 'c.id = a.id_controller')
                       ->join(array('p' => 'db_ACL'), 'p.id_action = a.id')
                       ->join(array('t' => 'db_roles'), 'p.id_role = t.id')
                       ->group('c.controller')
                       ->group('a.action');
        return $this->_fetch($select);
    }

    //Get ACL by ID Action and ID Role
    public function getACLByActionRole($idAction, $idRole) {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('p' => 'db_ACL'))
                       ->where('p.id_action = ?', $idAction)
                       ->where('p.id_role = ?', $idRole);
        return $this->_fetch($select);
    }

    //Get ACL By ID Role
    public function getACLByIDRole($idRole) {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from(array('c' => 'db_controller'))
                       ->join(array('a' => 'db_action'), 'c.id = a.id_controller')
                       ->join(array('p' => 'db_ACL'), 'p.id_action = a.id and p.id_role = ' . $idRole);
        return $this->_fetch($select);
    }

    //Get ACL Count
    public function getACLCount() {
        $select = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p' => 'db_ACL'), array('p.id_role'))
                        ->join(array('c' => 'db_controller'), 'p.id_action in (SELECT id FROM db_action WHERE id_controller = c.id) ', array('ACLCount' => 'count( * )', 'c.id'))
                        ->where('p.access = 1')
                        ->group('p.id_role')
                        ->group('c.id');
        return $this->_fetch($select);
    }

    //Insert ACL
    public function insertACL($idAction, $idRole, $access) {
        $insert = array('id_action' => $idAction, 'id_role' => $idRole, 'access' => $access);
        $this->_db->insert('db_ACL', $insert);
        return $this->_db->lastInsertId();
    }

    //Update ACL
    public function updateACL($id, $idAction, $idRole, $access) {
        $update = array('id_action' => $idAction, 'id_role' => $idRole, 'access' => $access);
        $this->_db->update('db_ACL', $update, 'id = ' . $id);
        return $this->_db->lastInsertId();
    }

    //Delete ACL by ID Action
    public function deleteACLByIDAction($id) {
        $this->_db->delete('db_ACL', 'id_action = ' . $id);
    }
}